package com.anlu.yue.config;

import com.anlu.yue.intercepor.LoggingInterceptor;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class RestTemplateConfig {


    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(200);             // 最大连接数为 200
        connectionManager.setDefaultMaxPerRoute(20);    // 每个路由的最大连接数为 20

        CloseableHttpClient httpClient = HttpClients.custom()   // 建 HTTP 客户端
                .setConnectionManager(connectionManager)
                .build();

        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(new LoggingInterceptor());
        RestTemplate build = builder
                .interceptors(interceptors)
                .errorHandler(new CustomResponseErrorHandler())
                .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(httpClient))
                .build();
        // 返回 RestTemplate
        return build;
    }

    @Bean
    public RestTemplate handleErrorRestTemplate(RestTemplateBuilder builder) {
        return builder
                .errorHandler(new CustomResponseErrorHandler())
                .build();
    }

}
